package com.zhipu.balance.utils

import android_serialport_api.SerialPortFinder
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.ShellUtils
import com.zhipu.balance.common.Constants
import com.zhipu.balance.listener.SerialPortListener
import tp.xmaihh.serialport.SerialHelper
import tp.xmaihh.serialport.bean.ComBean
import java.io.File

object OpenSerialPort {
    private var mSerialPortFinder = SerialPortFinder()
    private var mSerialHelper : SerialHelper? = null

    /**
     * 初始化打开串口
     */
    fun initSerialPortOpen(){
        var sPort = "/dev/ttyS1"
//        var sPort = "/dev/ttyS3"
        var mCommandResult = ShellUtils.execCmd(" chmod 666 ${sPort}",true)
        println("权限${GsonUtils.toJson(mCommandResult)}")
        when(mCommandResult.result){
            0 ->{
                mSerialHelper = object : SerialHelper(sPort,9600){
                    override fun onDataReceived(p0: ComBean?) {
                        if (p0?.bRec?.isNotEmpty() == true && p0?.bRec?.last() == 0x04.toByte()) {
                            mSerialPortListener?.itemClicked(p0?.bRec)
                        }
                    }
                }
                mSerialHelper?.open()
            }
        }
    }


    /**
     * 获取全部串口
     */
    fun getAllDevicesPath() : Array<String>{
        return mSerialPortFinder.allDevicesPath
    }

    private var mSerialPortListener: SerialPortListener? = null
    fun onSerialPortListener(serialPortListener: SerialPortListener?) {
        mSerialPortListener = serialPortListener
    }


    /**
     * 切换到3000
     */
    fun SwitchTo3000(){
        println("切换到3000")
        val sendContentBytes = ByteArray(3)
        sendContentBytes[0] = 0x4C
        sendContentBytes[1] = 0x0D
        sendContentBytes[2] = 0x0A
        if(mSerialHelper?.isOpen == true){
            mSerialHelper?.send(sendContentBytes)
            /**
             * 默认3000
             */
            Constants.TWO_PERHAPS_THREE = true
        }
    }


    /**
     * 切换到2000
     */
    fun SwitchTo2000(){
        println("切换到2000")
        val sendContentBytes = ByteArray(3)
        sendContentBytes[0] = 0x48
        sendContentBytes[1] = 0x0D
        sendContentBytes[2] = 0x0A
        if(mSerialHelper?.isOpen == true){
            mSerialHelper?.send(sendContentBytes)
            /**
             * 默认2000
             */
            Constants.TWO_PERHAPS_THREE = false
        }
    }


    /**
     * 重量輸出命令
     */
    fun output(){
        val sendContentBytes = ByteArray(3)
        sendContentBytes[0] = 0x52
        sendContentBytes[1] = 0x0D
        sendContentBytes[2] = 0x0A
        if(mSerialHelper?.isOpen == true){
            mSerialHelper?.send(sendContentBytes)
        }
    }

    /**
     * 綫性標定命令
     */
    fun calibration1000(){
        val sendContentBytes = ByteArray(3)
        sendContentBytes[0] = 0x43
        sendContentBytes[1] = 0x0D
        sendContentBytes[2] = 0x0A
        if(mSerialHelper?.isOpen == true){
            mSerialHelper?.send(sendContentBytes)
            InitializeUtils.getDefaultMaker()?.show("正在校准请稍后")
        }
    }

    /**
     * 500G定标
     * 0 零点标定
     * 1 500g标定
     * 2 1000g标定
     * 3 2000g标定
     * 4 3000g标定
     */
    fun Scaling500(){
        val sendContentBytes = ByteArray(3)
        sendContentBytes[0] = 0x31
        sendContentBytes[1] = 0x0D
        sendContentBytes[2] = 0x0A
        if(mSerialHelper?.isOpen == true){
            mSerialHelper?.send(sendContentBytes)
            InitializeUtils.getDefaultMaker()?.show("正在校准请稍后")
        }
    }





    /**
     * 取消定标
     */
    fun cancelCalibration() {
        val sendContentBytes = ByteArray(3)
        sendContentBytes[0] = 0x1B
        sendContentBytes[1] = 0x0D
        sendContentBytes[2] = 0x0A
        if(mSerialHelper?.isOpen == true){
            mSerialHelper?.send(sendContentBytes)
            InitializeUtils.getDefaultMaker()?.show("正在取消定标请稍后")
        }
    }


    /**
     * 置零命令
     */
    fun ZeroOrder(){
        val sendContentBytes = ByteArray(3)
        sendContentBytes[0] = 0x5A
        sendContentBytes[1] = 0x0D
        sendContentBytes[2] = 0x0A
        if(mSerialHelper?.isOpen == true){
            mSerialHelper?.send(sendContentBytes)
            InitializeUtils.getDefaultMaker()?.show("正在去皮请稍后")
        }
    }
}


